package juc.executors;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;

public class FutureExample2 {

    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        //testIsDone();
        //testCancel();
        testCancel2();
        //testCancel3();
    }

    /**
     *
     */
    private static void testIsDone() throws ExecutionException, InterruptedException, TimeoutException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Future<Integer> future = executorService.submit(() -> {
            //timeSleep(2);
            //return 10;
            throw new RuntimeException();
        });
        try {
            Integer result = future.get();
            System.out.println(result);
        } catch (ExecutionException e) {
            System.out.println(" is done " + future.isDone());
        }
    }

    /**
     * try to cancel maybe failed
     * <ul>
     * <li>task is completed already.</li>
     * <li>has already been cancelled.</li>
     * <li></li>
     * </ul>
     */
    private static void testCancel() throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        AtomicBoolean running = new AtomicBoolean(true);
        Future<Integer> future = executorService.submit(() -> {
            timeSleep(3);
            while (running.get()) {
            }
            return 10;
        });
        timeSleep(1);
        //System.out.println(future.get());
        System.out.println(future.cancel(true));
        //System.out.println(future.cancel(true));
        System.out.println(future.isDone());
        System.out.println(future.isCancelled());
    }

    private static void testCancel2() throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        Future<Integer> future = executorService.submit(() -> {
            while (!Thread.interrupted()) {
            }
            System.out.println("11111");
            return 10;
        });
        timeSleep(1);
        //cancel后任务会继续执行，一般cancel()配合interrupted()使用
        System.out.println(future.cancel(true));
        System.out.println(future.isDone());
        System.out.println(future.isCancelled());
        //cancel后不能获取返回的结果
        System.out.println(future.get());

    }

    private static void testCancel3() throws ExecutionException, InterruptedException {
        //如果整个线程长时间执行，cancel不掉的话，可以用设置守护线程的方式结束
        ExecutorService executorService = Executors.newCachedThreadPool(new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                thread.setDaemon(true);
                return thread;
            }
        });
        AtomicBoolean running = new AtomicBoolean(true);
        Future<Integer> future = executorService.submit(() -> {
            while (running.get()) {
            }
            System.out.println("11111");
            return 10;
        });
        timeSleep(1);
        //cancel后任务会继续执行，一般cancel()配合interrupted()使用
        System.out.println(future.cancel(true));
        System.out.println(future.isDone());
        System.out.println(future.isCancelled());
    }

    private static void timeSleep(long time) {
        try {
            TimeUnit.SECONDS.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
